package com.dys.ai.common.config;

import com.alibaba.nacos.common.packagescan.util.NestedExceptionUtils;
import com.dys.ai.common.api.ApiResponse;
import com.dys.ai.common.api.ResponseEntity;
import com.dys.ai.common.dto.BookingDetailsDTO;
import com.dys.ai.common.exception.CustomException;
import com.dys.ai.service.TicketService;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;

import java.time.LocalTime;
import java.util.function.Function;

/**
 * @author 疾风
 */
@Slf4j
@Configuration
public class TicketTool {

    @Autowired
    private TicketService ticketService;

    /**
     * 获取订单详情请求
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public record BookingDetailsRequest(String realName, String trainNumber) {
    }

    /**
     * Description 告诉大模型,什么对话才回调
     */
    @Bean
    @Description(value = "获取订单信息")
    public Function<BookingDetailsRequest, BookingDetailsDTO> getOrderDetails() {
        return request -> {
            try {
                return ticketService.getBookingDetailsByRealNameAndTrainNumber(request.realName(), request.trainNumber());
            } catch (CustomException e) {
                log.warn("Booking details: {}", NestedExceptionUtils.getMostSpecificCause(e).getMessage());
                return new BookingDetailsDTO(null, request.realName(), request.trainNumber(), null, null, null, null, null, null);
            }
        };
    }

    /**
     * 购票
     */
    public record BuyTicket(String realName, String departureStation, String arrivalStation,
                            LocalTime departureDate, String trainNumber, String seatTypeName) {
    }

    @Bean
    @Description(value = "购票")
    public Function<BuyTicket, ResponseEntity> buyTicket() {
        try {
            return request -> {
                ticketService.butTicketByUserInput(request.realName(),
                        request.departureStation(), request.arrivalStation(), request.departureDate(), request.trainNumber(), request.seatTypeName());
                return ApiResponse.success("购票成功", null);
            };
        } catch (CustomException e) {
            log.warn("购票: {}", NestedExceptionUtils.getMostSpecificCause(e).getMessage());
        }
        return null;
    }

    /**
     * 改签
     */
    public record ReBook(Long originalOrderId, Long newTrainId, Integer newCarriageNumber, String newSeatNumber) {
    }

    @Bean
    @Description(value = "改签")
    public Function<ReBook, ResponseEntity> reBook() {
        try {
            return request -> {
                ticketService.changeOrder(request.originalOrderId(), request.newTrainId(), request.newCarriageNumber(), request.newSeatNumber());
                return ApiResponse.success("改签成功", null);
            };
        } catch (RuntimeException e) {
            log.warn("改签: {}", NestedExceptionUtils.getMostSpecificCause(e).getMessage());
        }
        return null;
    }

    /**
     * 退票
     */
    public record RefundTicket(Long orderId) {
    }

    @Bean
    @Description(value = "退票")
    public Function<RefundTicket, ResponseEntity> refundTicket() {
        try {
            return request -> {
                ticketService.refundOrder(request.orderId());
                return ApiResponse.success("退票成功", null);
            };
        } catch (RuntimeException e) {
            log.warn("退票: {}", NestedExceptionUtils.getMostSpecificCause(e).getMessage());
        }
        return null;
    }
}
